package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysJudgeQuestionDao;
import com.groupfifteen.entity.SysJudgeQuestionEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysJudgeQuestionService")
public class SysJudgeQuestionServiceImpl extends ServiceImpl<SysJudgeQuestionDao, SysJudgeQuestionEntity> implements SysJudgeQuestionService {

    private final SysJudgeQuestionDao sysJudgeQuestionDao;

    public SysJudgeQuestionServiceImpl(SysJudgeQuestionDao sysJudgeQuestionDao) {
        this.sysJudgeQuestionDao = sysJudgeQuestionDao;
    }

    @Override
    public IPage<SysJudgeQuestionEntity> querySysJudgeQuestions(SysJudgeQuestionEntity sysJudgeQuestionEntity, int pageNo, int pageSize) {
        LambdaQueryWrapper<SysJudgeQuestionEntity> query = Wrappers.lambdaQuery();
        // 假设我们根据questionId进行查询，您可以根据实际需求添加更多的查询条件
        query.eq(sysJudgeQuestionEntity.getQuestionId() != null, SysJudgeQuestionEntity::getQuestionId, sysJudgeQuestionEntity.getQuestionId());

        // 确保页码和页大小有效
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }

        // 创建分页对象
        IPage<SysJudgeQuestionEntity> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        return baseMapper.selectPage(page, query);
    }



    @Override
    public Integer AddJudgeQuestion(SysJudgeQuestionEntity sysJudgeQuestionEntity) {
        return baseMapper.insert(sysJudgeQuestionEntity);
    }

    @Override
    public Integer DeleteJudgeQuestion(Integer question_id) {
        return baseMapper.deleteById(question_id);
    }

    @Override
    public SysJudgeQuestionEntity GetJudgeQuestion(Integer question_id) {
        return baseMapper.selectById(question_id);
    }

    @Override
    public Integer UpdateJudgeQuestion(SysJudgeQuestionEntity sysJudgeQuestionEntity) {
        return baseMapper.updateById(sysJudgeQuestionEntity);
    }

    /**
     * 通过试卷id查询问题
     *
     * @param paperId 试卷id
     * @return 问题
     */
    @Override
    public List<SysJudgeQuestionEntity> findByIdAndType(Integer paperId) {
        return sysJudgeQuestionDao.findByIdAndType(paperId);
    }

    /**
     * 查询最后一个判断题id
     *
     * @return 最后一个判断题
     */
    @Override
    public SysJudgeQuestionEntity findOnlyQuestionId() {
        return baseMapper.findOnlyQuestionId();
    }
    /**
     *
     * 通过学科查询
     * @param subject 学科
     * @param pageNo 分页数
     * @return 题目id
     */
    @Override
    public List<Integer> findBySubject(String subject, Integer pageNo) {
        return sysJudgeQuestionDao.findBySubject(subject,pageNo);
    }

}